<?php

class ome_mdl_refund_apply extends dbeav_model{
	var $has_many = array(
	   'delivery' => 'delivery'
	);
    var $pay_type = array (
        'online' => '在线支付',
        'offline' => '线下支付',
        'deposit' => '预存款支付',
      );
      
    function _filter($filter,$tableAlias=null,$baseWhere=null){
        if(isset($filter['order_bn'])){
            $orderObj = &$this->app->model("orders");
            $rows = $orderObj->getList('order_id', array('order_bn|has'=>$filter['order_bn']), 0, -1);
            foreach($rows as $row){
                $orderId[] = $row['order_id'];
            }
            if (empty($orderId)){
                $orderId[] = '0';
            }
            $where .= ' AND order_id IN ('.implode(',', $orderId).')';
            unset($filter['order_bn']);
        }
        if (isset($filter['ome_uname'])){
            $memberObj = &$this->app->model("members");
            $rows = $memberObj->getList('ome_id',array('uname|has'=>$filter['ome_uname']));
            $memberId[] = 0;
            foreach($rows as $row){
                $memberId[] = $row['ome_id'];
            }

            $orderObj = &$this->app->model("orders");
            $rows = $orderObj->getList('order_id', array('ome_id'=>$memberId));
            $orderId[] = 0;
            foreach($rows as $row){
                $orderId[] = $row['order_id'];
            }
            $where .= ' AND order_id IN ('.implode(',', $orderId).')';
            unset($filter['ome_uname']);
        }
        if (isset($filter['ship_name'])){
            $orderObj = &$this->app->model("orders");
            $rows = $orderObj->getList('order_id', array('ship_name|has'=>$filter['ship_name']));
            $orderId[] = 0;
            foreach($rows as $row){
                $orderId[] = $row['order_id'];
            }
            $where .= ' AND order_id IN ('.implode(',', $orderId).')';
            unset($filter['ship_name']);
        }
        return parent::_filter($filter,$tableAlias,$baseWhere).$where;
    }

    function refund_apply_detail($refapply_id){
    	$refapply_detail = $this->dump($refapply_id);
        
        if ($refapply_detail['payment']){
    	    $sql = "SELECT custom_name FROM sdb_ome_payment_cfg WHERE id=".$refapply_detail['payment'];
    	    $payment_cfg = $this->db->selectrow($sql);
            $refapply_detail['payment_name'] = $payment_cfg['custom_name'];
        }else {
            $refapply_detail['payment_name'] = '';
        }
    	
    	$refapply_detail['type'] = $this->pay_type[$refapply_detail['pay_type']];
    	return $refapply_detail;
    }
    
    /* create_refund_apply 添加申请退款单
     * @param sdf $sdf
     * @return sdf
     */
    function create_refund_apply(&$sdf){
        $this->save($sdf);
    }

    function save(&$refund_data,$mustUpdate=NULL){
    	return parent::save($refund_data,$mustUpdate,true);
    }
    
    /**
     * 快捷搜索
     */
    function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'order_bn' => '订单号',
            'ome_uname'=>app::get('base')->_('用户名'),
            'ship_name'=>app::get('base')->_('收货人'),
        );
        return array_merge($parentOptions,$childOptions);
    }
    
    /**
     * 检查是否要将订单设为取消
     * 只有 全额退款并且为未发货的订单才会取消
     */
    function check_iscancel($order_id,$memo=null){
    	$oOrder = app::get('ome')->model('orders');
        $order_detaillist = $oOrder->dump($order_id);
          //只有未发货的才会取消订单
          if($order_detaillist['ship_status'] == 0){
          //增加订单取消的流程
          $memo = $memo?$memo:'订单全额退款后取消！';
          $oOrder->cancel($order_id,$memo);
       }
    }
    
    /*
     * 退款申请单号
     *
     * @return 退款单号
     */
     function gen_id(){
        $i = rand(0,9999);
        do{
            if(9999==$i){
                $i=0;
            }
            $i++;
            $refund_apply_bn = date("YmdH").'14'.str_pad($i,6,'0',STR_PAD_LEFT);
            $row = $this->db->selectrow('select refund_apply_bn from sdb_ome_refund_apply where refund_apply_bn =\''.$refund_apply_bn.'\'');
        }while($row);
        return $refund_apply_bn;
    }
    
}
?>